/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307, USA.
 *
 * http://www.gnu.org/copyleft/gpl.html
 */
package l1j.server.server.model;

import static l1j.server.server.model.skill.L1SkillId.DRAGON_COOLTIME_DIA;
import static l1j.server.server.model.skill.L1SkillId.DRAGON_COOLTIME_EME;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;

import l1j.server.L1DatabaseFactory;
import l1j.server.server.model.Instance.L1PcInstance;
import l1j.server.server.serverpackets.S_PacketBox;
import l1j.server.server.utils.SQLUtil;

// Referenced classes of package l1j.server.server.model:
// L1Cooking

public class L1CoolTimeBuff {

	public static void cooltime(L1PcInstance pc) {
		Connection con = null;
		PreparedStatement pstm = null;
		ResultSet rs = null;
		try {
			con = L1DatabaseFactory.getInstance().getConnection();
			pstm = con.prepareStatement("SELECT skill_id, endtime FROM character_buff WHERE char_obj_id=?");
			pstm.setInt(1, pc.getId());
			rs = pstm.executeQuery();
			while (rs.next()) {
				int skillid = rs.getInt("skill_id");
				Timestamp endtime = rs.getTimestamp("endtime");
				int cooltime = 0;
				
				switch (skillid) {
				
				case DRAGON_COOLTIME_DIA:
					cooltime = (int)(endtime.getTime() - System.currentTimeMillis());
					if (cooltime >= 0) {
					pc.sendPackets(new S_PacketBox(S_PacketBox.DRAGON_COOLTIME, 1, cooltime / 1000));
					pc.getSkillEffectTimerSet().setSkillEffect(skillid, cooltime);
					} else {
						pc.getSkillEffectTimerSet().removeSkillEffect(skillid);
					}
					break;
				
				case DRAGON_COOLTIME_EME:
					cooltime = (int)(endtime.getTime() - System.currentTimeMillis());
					if (cooltime >= 0) {
					pc.sendPackets(new S_PacketBox(S_PacketBox.DRAGON_COOLTIME, 2, cooltime / 1000));
					pc.getSkillEffectTimerSet().setSkillEffect(skillid, cooltime);
					} else {
						pc.getSkillEffectTimerSet().removeSkillEffect(skillid);
					}
					break;
				default:
					break;
				}
				
			}
			
		} catch (SQLException e) {
		} finally {
			SQLUtil.close(rs);
			SQLUtil.close(pstm);
			SQLUtil.close(con);
		}
	}
}